import os
import pandas as pd
from datetime import datetime, timedelta

# 设置 CSV 文件夹路径和输出路径
folder_path = "D:\\mydata\\生态项目相关\\数据集\\好快\\landset8\\Normalization rsei folder"
output_folder = "D:\\mydata\\生态项目相关\\数据集\\好快\\landset8"
output_file = os.path.join(output_folder, "summary_statistics.csv")
filled_output_file = os.path.join(output_folder, "summary_statistics_filled_nan.csv")

# 需要统计的字段
target_columns = ['NDVI', 'WET', 'NDBSI', 'LST', 'PMDI', 'CSI', 'RSEI']

summary_data = []

# 遍历所有 CSV 文件
for filename in os.listdir(folder_path):
    if filename.endswith('.csv'):
        file_path = os.path.join(folder_path, filename)

        try:
            # 提取并格式化日期
            date_str = filename.split('_')[1]
            date_formatted = datetime.strptime(date_str, '%Y%m%d').strftime('%Y-%m-%d')

            # 修正特定日期
            if date_formatted == "2013-04-06":
                date_formatted = "2013-04-04"

            # 读取 CSV 文件
            df = pd.read_csv(file_path)

            # 只提取目标列并计算平均
            data = df[target_columns]
            means = data.mean().to_dict()
            means['date'] = date_formatted
            summary_data.append(means)

        except Exception as e:
            print(f"处理文件 {filename} 出错：{e}")

# 创建 DataFrame 并整理列顺序
summary_df = pd.DataFrame(summary_data)
cols = ['date'] + [col for col in summary_df.columns if col != 'date']
summary_df = summary_df[cols]

# 按日期排序
summary_df['date'] = pd.to_datetime(summary_df['date'])
summary_df = summary_df.sort_values(by='date').reset_index(drop=True)

# 保存原始统计结果
summary_df.to_csv(output_file, index=False)
print(f"原始统计结果已保存到 {output_file}")

# 生成完整日期序列（按 16 天间隔）
start_date = summary_df['date'].min()
end_date = summary_df['date'].max()
full_dates = pd.date_range(start=start_date, end=end_date, freq='16D')

# 创建完整日期的 DataFrame，并合并原始数据
full_df = pd.DataFrame({'date': full_dates})
filled_df = pd.merge(full_df, summary_df, on='date', how='left')

# 保存补全后的文件
filled_df.to_csv(filled_output_file, index=False)
print(f"补全后的统计结果已保存到 {filled_output_file}")
